//
//  SNBrandsadCell.m
//  app
//
//  Created by 王少宁 on 15/8/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNBrandsadCell.h"

@interface SNBrandsadCell () <UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView * myScrollView;
@property (nonatomic, retain) NSArray * brandsadArray;
@property (nonatomic, retain) UIPageControl * pageControl;
@property (nonatomic, assign) int pageCount;
@property (nonatomic, assign) BOOL changPicsSelf;
@end

@implementation SNBrandsadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier shopBaseModel:(SNShopBaseModel *)model
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier shopBaseModel:model]) {
        _myScrollView = [[UIScrollView alloc] init];
        _myScrollView.delegate = self;
        _myScrollView.bounces = NO;
        _myScrollView.showsHorizontalScrollIndicator = NO;
        _myScrollView.showsVerticalScrollIndicator = NO;
        [self.contentView addSubview:_myScrollView];
        [self cellWithModel:model];
    }
    return self;
}

- (void)cellWithModel:(SNShopBaseModel *)shopModel
{
    SNShopBrandsadModel * model = (SNShopBrandsadModel *)shopModel;
    
    if (model.cellHeight == 0) return;
    
    _myScrollView.frame = CGRectOf(CGPointMake(0, 0), [self.contentView.ubo ow:SCREEN_WIDTH oh:model.cellHeight]);
    
    if (model.brandsadArray.count == 1) { //图片不需要轮播
        
        SNShopBrandsadSubModel * subModel = model.brandsadArray.firstObject;
        UIButton * picBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        picBtn.adjustsImageWhenHighlighted = NO;
        picBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, model.cellHeight);
        [picBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:subModel.ad_pic] forState:UIControlStateNormal];
        picBtn.tag = 1;
        [picBtn addTarget:self action:@selector(picBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_myScrollView addSubview:picBtn];
        
    } else { //图片需要轮播
        self.brandsadArray = model.brandsadArray;
        
        _myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * (model.brandsadArray.count+2), model.cellHeight);
        
        SNShopBrandsadSubModel * itemModelLast = model.brandsadArray.lastObject;
        UIButton * buttonFirst = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonFirst.adjustsImageWhenHighlighted = NO;
        buttonFirst.frame = CGRectMake(0, 0, SCREEN_WIDTH, model.cellHeight);
        [buttonFirst sd_setBackgroundImageWithURL:[NSURL URLWithString:itemModelLast.ad_pic] forState:UIControlStateNormal];
        [_myScrollView addSubview:buttonFirst];
        
        for (int i = 0; i < model.brandsadArray.count; i++) {
            SNShopBrandsadSubModel * itemModel = model.brandsadArray[i];
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.adjustsImageWhenHighlighted = NO;
            button.frame = CGRectMake((i+1)*SCREEN_WIDTH, 0, SCREEN_WIDTH, model.cellHeight);
            [button sd_setBackgroundImageWithURL:[NSURL URLWithString:itemModel.ad_pic] forState:UIControlStateNormal];
            button.tag = i+1;
            [button addTarget:self action:@selector(picBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [_myScrollView addSubview:button];
        }
        
        SNShopBrandsadSubModel * itemModelFirst = model.brandsadArray.firstObject;
        UIButton * buttonLast = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonLast.adjustsImageWhenHighlighted = NO;
        buttonLast.frame = CGRectMake((model.brandsadArray.count+1)*SCREEN_WIDTH, 0, SCREEN_WIDTH, model.cellHeight);
        [buttonLast sd_setBackgroundImageWithURL:[NSURL URLWithString:itemModelFirst.ad_pic] forState:UIControlStateNormal];
        [_myScrollView addSubview:buttonLast];
        
        _myScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        self.changPicsSelf = _myScrollView.pagingEnabled = YES;
        
        _pageControl = [[UIPageControl alloc] init];
        _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.6];
        _pageControl.currentPageIndicatorTintColor = [XCommon hexStringToColor:@"#f16e02"];
        _pageControl.numberOfPages = model.brandsadArray.count;
        [_pageControl sizeToFit];
        _pageControl.center = CGPointMake(self.contentView.center.x, _myScrollView.ubo.height - 20);
        [self.contentView addSubview:_pageControl];
        
        [self performSelector:@selector(scrollViewChangePic) withObject:nil afterDelay:kBrandsadCellRoundTime];
        
    }
}

- (void)picBtnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(itemClickWithShopBaseModel:row:index:)]) {
        [self.delegate itemClickWithShopBaseModel:self.shopBaseModel row:0 index:btn.tag - 1];
    }
}

- (void)scrollViewChangePic
{
    if (self.changPicsSelf) {
        
        _pageCount = _pageCount % self.brandsadArray.count;
        if (_myScrollView.contentOffset.x == self.brandsadArray.count * SCREEN_WIDTH) {
            
            [UIView animateWithDuration:0.25 animations:^{
                [_myScrollView setContentOffset:CGPointMake((self.brandsadArray.count+1) * SCREEN_WIDTH, 0) animated:NO];
            } completion:^(BOOL finished) {
                // 回到第一个页面
                [_myScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:NO];
            }];
        } else {
            [_myScrollView setContentOffset:CGPointMake((_pageCount+1) * SCREEN_WIDTH, 0) animated:YES];
        }
        
        self.pageCount++;
        
        __weak typeof(self) weakSelf = self;
        [weakSelf performSelector:@selector(scrollViewChangePic) withObject:nil afterDelay:2.0f];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    DLog(@"减速停止");
    if (scrollView.contentOffset.x == 0) {
        _myScrollView.contentOffset = CGPointMake(self.brandsadArray.count * SCREEN_WIDTH, 0);
    } else if (scrollView.contentOffset.x == (self.brandsadArray.count+1) * SCREEN_WIDTH) {
        _myScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
    
    _pageCount = _pageControl.currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width - 1;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float e = scrollView.contentOffset.x / SCREEN_WIDTH;
    int a = e;
    if (a == e) {
        _pageControl.currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width - 1;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.changPicsSelf = NO;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    DLog(@"停止拖拽");
    self.changPicsSelf = YES;
}

@end
